class MyPageHeader extends BaseEditMaster {
    constructor(dataId, currentTaskPrivileges, nextTaskPrivileges, pageStatus, pageListData,
            returnUrl, fetchDataUrl, saveUrl, exportUrl, printUrl) {
        super(dataId, currentTaskPrivileges, nextTaskPrivileges, pageStatus, pageListData,
                returnUrl, fetchDataUrl, saveUrl, exportUrl, printUrl);
    }
    // 得到数据
    GetEntityData() {
        var item = {};
        item['productId'] = $("#ProductId").val();
        item['catalogueId'] = $("#CatalogueId").val();
        item['enterpriseId'] = $("#EnterpriseId").val();
        item['styleNo'] = $.trim($("#StyleNo").val());
        item['designNo'] = formatNullInputValue($.trim($("#DesignNo").val()));
        item['serialNo'] = formatNullInputValue($.trim($("#SerialNo").val()));
        item['gender'] = formatNullInputValue($.trim($("#Gender").val()));
        item['barcode'] = formatNullInputValue($.trim($("#Barcode").val()));
        item['productName'] = formatNullInputValue($.trim($("#ProductName").val()));
        item['brand'] = formatNullInputValue($.trim($("#Brand").val()));
        item['productTypeId'] = $.trim($("#ProductTypeId").val());
        item['costPrice'] = formatNullInputValue($.trim($("#CostPrice").val()));
        item['salePrice'] = formatNullInputValue($.trim($("#SalePrice").val()));
        item['customerId'] = $.trim($("#CustomerId").val());
        item['suitFlag'] = $.trim($("#SuitFlag").is(":checked") ? "1" : "0");
        item['suitComponetOrder'] = formatNullInputValue($.trim($("#SuitComponetOrder").val()));
        item['years'] = formatNullInputValue($.trim($("#Years").val()));
        item['seasonId'] = formatNullInputValue($.trim($("#SeasonId").val()));
        item['uomId'] = formatNullInputValue($.trim($("#UomId").val()));
        item['pattern'] = formatNullInputValue($.trim($("#Pattern").val()));
        item['mainMaterial'] = formatNullInputValue($.trim($("#MainMaterial").val()));
        item['endUserId'] = $("#EndUserId").val();
        item['description'] = formatNullInputValue($.trim($("#Description").val()));
        item['updatedBy'] = $("#UserId").val();

        return JSON.stringify(item);
    }
    FillData(data) {
        $("#ProductId").val(data.productId);
        $("#CatalogueId").val(data.catalogueId);
        $("#CatalogueName").val(data.catalogueName);
        $("#EnterpriseId").val(data.enterpriseId);
        $("#EnterpriseName").val(data.enterpriseName);
        $("#StyleNo").val(data.styleNo);
        $("#DesignNo").val(data.designNo);
        $("#SerialNo").val(data.serialNo);
        $("#Gender").val(data.gender);
        $("#Barcode").val(data.barcode);
        $("#ProductName").val(data.productName);
        $("#Brand").val(data.brand);
        $("#ProductTypeId").val(data.productTypeId);
        $("#ProductTypeName").val(data.productTypeName);
        $("#CostPrice").val(data.costPrice);
        $("#SalePrice").val(data.salePrice);
        $("#CustomerId").val(data.customerId);
        $("#CustomerName").val(data.customerName);
        if (parseInt(data.suitFlag) === 1) {
            $("#SuitFlag").prop("checked", true);
        } else {
            $("#SuitFlag").prop("checked", false);
        }
        $("#SuitComponetOrder").val(data.suitComponetOrder);
        $("#Years").val(data.years);
        $("#SeasonId").val(data.seasonId);
        $("#SeasonName").val(data.seasonName);
        $("#UomId").val(data.uomId);
        $("#UomName").val(data.uomName);
        $("#Pattern").val(data.pattern);
        $("#MainMaterial").val(data.mainMaterial);
        $("#EndUserId").val(data.endUserId);
        $("#EndUserName").val(data.endUserName);
        $("#Description").val(data.description);
        $("#NodeText").val(data.catalogueName);

        // 最后的审核人、审核日期
        //$("#CheckedBy").val(item.checkedBy);
        //$("#CheckedByName").val(item.checkedByName);
        //$("#CheckedDate").val(item.checkedDate === null ? '' : item.checkedDate);
        //$("#CheckedStatus").val(item.checkedStatus);
        //$('#FlowOrderNo').val(item.flowOrderNo);
        this.currentTaskPrivileges = data.currentTaskPrivileges;
        this.nextTaskPrivileges = data.nextTaskPrivileges;
        this.unCheckFlag = data.unCheckFlag;

        this.SetPageElementStatus();
        $("#StyleNo").focus();
    }
    SetPageElementStatus() {
        super.SetPageElementStatus(function () {
            //$("#ButSave").hide();
        }, function () {
            //$("#ButSave").show();
        }, function () {

        });
    }
    Save(flag) {
        super.Save(function (data, $self) {
            if (data > 0) {
                if (parseInt($self.pageStatus) === 1) {
                    $("#ProductId").val(data);   // 设置刚才插入的记录ID
                    $self.dataId = data;
                    $self.pageStatus = 2;
                }
                if (flag === 0) {
                    $self.New();
                }
            }
        });
    }
    OnNew() {
        let item = this.HoldEntityData();
        // 默认设置一些值
        $('#ProductId').val("0");
        super.OnNew();

        this.PasteEntityData(item);
        // 初始化所属材料分类
        $('#CatalogueId').val($("#NodeId").val());
        $('#CatalogueName').val($("#NodeText").val());
        $("#StyleNo").focus();
    }
    // 保留当前录入内容，避免重复录入
    HoldEntityData() {
        let item = {};
        item['enterpriseId'] = $("#EnterpriseId").val();
        item['enterpriseName'] = $("#EnterpriseName").val();
        item['designNo'] = $.trim($("#DesignNo").val());
        item['serialNo'] = $.trim($("#SerialNo").val());
        item['gender'] = $.trim($("#Gender").val());
        item['brand'] = $.trim($("#Brand").val());
        item['productTypeId'] = $.trim($("#ProductTypeId").val());
        item['productTypeName'] = $.trim($("#ProductTypeName").val());
        item['customerId'] = $.trim($("#CustomerId").val());
        item['customerName'] = $.trim($("#CustomerName").val());
        item['suitFlag'] = $.trim($("#SuitFlag").is(":checked") ? "1" : "0");
        item['years'] = $.trim($("#Years").val());
        item['seasonId'] = $.trim($("#SeasonId").val());
        item['seasonName'] = $.trim($("#SeasonName").val());
        item['uomId'] = $.trim($("#UomId").val());
        item['uomName'] = $.trim($("#UomName").val());
        item['pattern'] = $.trim($("#Pattern").val());
        item['mainMaterial'] = $.trim($("#MainMaterial").val());
        item['endUserId'] = $("#EndUserId").val();
        item['endUserName'] = $("#EndUserName").val();

        return item;
    }
    PasteEntityData(item) {
        $("#EnterpriseId").val(item['enterpriseId']);
        $("#EnterpriseName").val(item['enterpriseName']);
        $.trim($("#DesignNo").val(item['designNo']));
        $("#SerialNo").val(item['serialNo']);
        $("#Gender").val(item['gender']);
        $("#Brand").val(item['brand']);
        $("#ProductTypeId").val(item['productTypeId']);
        $("#ProductTypeName").val(item['productTypeName']);
        $("#CustomerId").val(item['customerId']);
        $("#CustomerName").val(item['customerName']);
        if (parseInt(item['suitFlag']) === 1) {
            $("#SuitFlag").prop("checked", true);
        } else {
            $("#SuitFlag").prop("checked", false);
        }
        $("#Years").val(item['years']);
        $("#SeasonId").val(item['seasonId']);
        $("#SeasonName").val(item['seasonName']);
        $("#UomId").val(item['uomId']);
        $("#UomName").val(item['uomName']);
        $("#Pattern").val(item['pattern']);
        $("#MainMaterial").val(item['mainMaterial']);
        $("#EndUserId").val(item['endUserId']);
        $("#EndUserName").val(item['endUserName']);
    }
    DoChoiceProductType() {
        let _choice = new CommonChoice();
        _choice.DoChoiceProductType(0, "选择产品品类", (data) => {
            if (data) {
                // 判断是否数据发生了改变如果改变则设置改变标记
                if ($("#ProductTypeName").val() !== data[1]) {
                    $("#form_data").data("changed", true);
                }
                $("#ProductTypeId").val(data[0]);
                $("#ProductTypeName").val(data[1]);
                $("#ProductTypeName").focus();
            }
        });
    }
    DoChoiceSeason() {
        let _choice = new CommonChoice();
        _choice.DoChoiceSeason(0, '选择产品季节', (data) => {
            if (data) {
                // 判断是否数据发生了改变如果改变则设置改变标记
                if ($("#SeasonName").val() !== data[1]) {
                    $("#form_data").data("changed", true);
                }
                $("#SeasonId").val(data[0]);
                $("#SeasonName").val(data[1]);
                $("#SeasonName").focus();
            }
        });
    }
    DoChoiceCustomer() {
        let _choice = new CommonChoice();
        let params = {};
        params['Flag'] = 0;
        params['CurrentUserId'] = null;
        params['NotInGroupId'] = null;
        _choice.DoChoiceCustomer(params, '选择客户', (data, _this) => {
            if (data) {
                // 判断是否数据发生了改变如果改变则设置改变标记
                if ($("#CustomerName").val() !== data[1]) {
                    $("#form_data").data("changed", true);
                }
                $("#CustomerId").val(data[0]);
                $("#CustomerName").val(data[1]);
            }
        });
    }
    DoChoiceEndUser() {
        let _choice = new CommonChoice();
        _choice.DoChoiceEndUser(0, '选择终端客户', (data, _this) => {
            if (data) {
                // 判断是否数据发生了改变如果改变则设置改变标记
                if ($("#EndUserName").val() !== data[1]) {
                    $("#form_data").data("changed", true);
                }
                $("#EndUserId").val(data[0]);
                $("#EndUserName").val(data[1]);
            }
        });
    }
    DoChoiceUom() {
        let _choice = new CommonChoice();
        _choice.DoChoiceUom(0, '选择单位', (data) => {
            if (data) {
                // 判断是否数据发生了改变如果改变则设置改变标记
                if ($("#UomName").val() !== data[1]) {
                    $("#form_data").data("changed", true);
                }
                $("#UomId").val(data[0]);
                $("#UomName").val(data[1]);
                $("#UomName").focus();
            }
        });
    }
    DoChoiceCatalogue() {
        let _choice = new CommonChoice();
        _choice.DoChoiceCatalogue('/master/product/catalogue/choice.html', '选择所属目录', (data) => {
            if (data) {
                // 判断是否数据发生了改变如果改变则设置改变标记
                if ($("#CatalogueName").val() !== data[1]) {
                    $("#form_data").data("changed", true);
                }
                $("#CatalogueId").val(data[0]);
                $("#CatalogueName").val(data[1]);
                $("#CatalogueName").focus();
            }
        });
    }
}

var _InstancePageHeader = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    let _pageStatus = $("#Action").val();
    let _dataId = $("#ProductId").val();

    let product_list_result = $.cookie("product_list_result") === undefined ? "" : $.cookie("product_list_result");
    let _pageListData = [];
    if (product_list_result !== "") {
        _pageListData = product_list_result.split(',');
        $("#rec_total_rows").text(_pageListData.length);
        $("#rec_cur_row").text($.inArray(_dataId, _pageListData) + 1);

        if (_pageListData.length > 0) {
            let index = $.inArray(_dataId, _pageListData);
            if (index === 0) {
                $("#ButPrev").attr("disabled", "disabled");
            } else {
                $("#ButNext").removeAttr("disabled");
            }
            if (index === (_pageListData.length - 1)) {
                $("#ButNext").attr("disabled", "disabled");
            } else {
                $("#ButPrev").removeAttr("disabled");
            }
        }
    } else {
        $("#rec_total_rows").text("1");
        $("#rec_cur_row").text("1");
    }

    _InstancePageHeader = new MyPageHeader(_dataId, 0, 0, _pageStatus, _pageListData,
            "/master/product/list.html",
            "/master/product/fetchByKey.html",
            "/master/product/save.html",
            "/master/product/export.html",
            "/master/product/print.html");

    _InstancePageHeader.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    var w = $(window).width();
    var h = $(window).height();

    $(".data__table_wrapper").css({width: w - 40, height: h - 120});
}

//-----------------------------------------------------------------------------
// 验证函数
//-----------------------------------------------------------------------------
$.validator.setDefaults({
    ignore: 'hidden, [reaonly=readonly]'
});
$.validator.addMethod("valueNotEquals", function (value, element, arg) {
    return arg !== value;
}, "Value must not equal arg.");

$(function () {
    $("#form_data").validate({
        // Step 1：设置验证规则和错误消息
        rules: {
            _StyleNo: "required",
            _SerialNo: "required",
            _ProductName: "required",
            _Gender: {
                valueNotEquals: "-1",
                required: true
            },
            _TypeOfProductName: "required",
            _CostPrice: {
                number: true
            },
            _SalePrice: {
                number: true
            },
            _CustomerName: "required",
            _UomName: "required",
            _CatalogueName: "required",
            _EnterpriseName: "required"
        },
        messages: {
            _StyleNo: "请填写款号。",
            _SerialNo: "请填写系列号。",
            _ProductName: "请填写产品名称。",
            _Gender: {
                valueNotEquals: "请选择性别。",
                required: "请选择性别。"
            },
            _TypeOfProductName: "请填写产品品类。",
            _CostPrice: {
                number: "请输入正确的数字。"
            },
            _SalePrice: {
                number: "请输入正确的数字。"
            },
            _CustomerName: "请填写客户。",
            _UomName: "请填写计量单位。",
            _CatalogueName: "请选择产品所属目录。",
            _EnterpriseName: "请选择接单贸易公司。"
        },
        // Step 2：设置验证触发事件
        onclick: false,
        onfocusout: false,
        onfocusin: false,
        onkeyup: false,
        onkeydown: false,
        focusInvalid: false,
        // Step 3：设置错误信息提示
        showErrors: window.showErrors  //使用自定义的错误显示方法
    });
});